class Observer {
  constructor(data) {
    this.walk(data)
  }
  walk(data) {
    // 判断data是否是对象
    if (!data || typeof data !== 'object') return
    //遍历data所有属性，转成getter/setter
    Object.keys(data).forEach((key) => {
      this.defineReactive(data, key, data[key])
    })
  }
  defineReactive(data, key, v) {
    const _this = this
    // 负责收集依赖，并发送通知
    let dep = new Dep()
    this.walk(v)
    Object.defineProperty(data, key, {
      enumerable: true,
      configurable: true,
      get(key) {
        // 收集依赖
        Dep.target && dep.addSub(Dep.target)
        return v
      },
      set(newValue) {
        if (v === newValue) return
        v = newValue
        _this.walk(newValue)
        // 发送通知
        dep.notify()
      },
    })
  }
}
